package com.qdu.web.controller.bussiness;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.qdu.bussiness.constant.BussinessConstant;
import com.qdu.bussiness.domain.BusTicketPurchase;
import com.qdu.bussiness.service.IBusTicketPurchaseService;
import com.qdu.common.core.domain.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 数据统计
 */
@RestController
@RequestMapping("/data/statistics")
public class DataStatisticsController {

    @Autowired
    private IBusTicketPurchaseService busTicketPurchaseService;

    /**
     * 获取当天售票情况数据 用于绘制统计图
     * @return
     */
    @GetMapping("/ticket")
    public AjaxResult getTodayTicketSales(){
        List<BusTicketPurchase> todayAllTicketPurchase =
                busTicketPurchaseService.getTodayAllTicketPurchase();
        // 对数据进行清洗
        int fourHoursTicket = 0;
        int eightHoursTicket = 0;
        for (BusTicketPurchase busTicketPurchase : todayAllTicketPurchase) {
            if(busTicketPurchase.getTicketType().equals(BussinessConstant.TICKET_TYPE_FOUR_HOURS)){
                fourHoursTicket ++ ;
            }else if(busTicketPurchase.getTicketType().equals(BussinessConstant.TICKET_TYPE_EIGHT_HOURS)){
                eightHoursTicket ++;
            }
        }
        JSONObject fourObject = new JSONObject();
        fourObject.put("value", fourHoursTicket);
        fourObject.put("name", "半天票");

        JSONObject eightObject = new JSONObject();
        eightObject.put("value", eightHoursTicket);
        eightObject.put("name", "全天票");

        JSONArray jsonArray = new JSONArray();
        jsonArray.add(fourObject);
        jsonArray.add(eightObject);

        return AjaxResult.success(jsonArray);
    }
}
